package com.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Slf4j
public class HttpFormUtil {

    public static String formCommit(String type,String urlString, String content,String boundary){
        HttpURLConnection connection = null;
        OutputStream outputStream = null;
        InputStreamReader ir = null;
        BufferedReader br = null;
        PrintWriter writer = null;
        for (int i = 0; i < 1; i++) {
            try {
                URL url = new URL(urlString);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                // 设置请求方法为 POST
                connection.setRequestMethod("POST");
                String boundary2 = "multipart/form-data; boundary=" + boundary;
                // 设置请求属性，表明是 form-data 请求
             //   connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryQ0rzq3BR5c3dQgNN");
                connection.setRequestProperty("Content-Type", boundary2);
                // 创建输出流，向服务器写入数据
                outputStream = connection.getOutputStream();
                writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                // 构建表单数据
                writer.append(content);
                writer.flush();
                StringBuilder stringBuffer = new StringBuilder();
                ir  = new InputStreamReader(connection.getInputStream());
                br = new BufferedReader(ir) ;
                String line;
                while ((line = br.readLine()) != null) {
                    stringBuffer.append(line);
                }
                br.close();
                ir.close();
                java.lang.String str = stringBuffer.toString();
                String str2 = new String(str.getBytes(), StandardCharsets.UTF_8);
           //     System.out.println("str2: "+ str2);
           //     log.info("str2:"+ str2);
                if (!StringUtils.isEmpty(str2)){
                    if (str2.contains("成功") || str2.contains("已完成") || str2.contains("200") || str2.contains("已抽奖") || str2.contains("重复")){
                        return str2;
                    }
                }
         //       System.out.println("type:"+ type +", "+ str2);
                // 关闭连接
                writer.close();
            }catch (Exception e){
                log.error("form 提交异常:,",e);
            }finally {
                try {
                    if (br != null){
                        br.close();
                    }
                    if (ir != null){
                        ir.close();
                    }
                    if (outputStream != null){
                        outputStream.close();
                    }
                    if (connection != null){
                        connection.disconnect();
                    }
                    if (writer != null){
                        writer.close();
                    }
                }catch (Exception e){
                    log.info("关流:",e);
                }
            }
            try {
                Thread.sleep(5000);
            }catch (Exception e){
            }
        }
        return "失败";
    }


}
